package com.lfg.leetcode.leetcode100;


public class No152maxProduct {

    public static void main(String[] args) {
        int ret = maxProduct(new int[]{1, 10, -2});
        System.out.println(ret);
    }

    public static int maxProduct(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        //局部最小和局部最大
        int min = nums[0];
        int max = min;
        int ret = min;
        for (int i = 1; i < nums.length; i++) {
            int tmpMin = min * nums[i];
            int tmpMax = max * nums[i];
            ret = Math.max(Math.max(tmpMax, tmpMin), Math.max(ret, nums[i]));
            max = Math.max(Math.max(tmpMax, tmpMin), nums[i]);
            min = Math.min(Math.min(tmpMax, tmpMin), nums[i]);
        }
        return ret;
    }

}
